package org.myhomework.dao;

import org.myhomework.entity.Teacher;
import org.myhomework.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public final class TeacherDao {

    public static List<Teacher> getTeacherList(Integer user_id) {
        try (Connection connection = JdbcUtil.getConnection()) {
            PreparedStatement preparedStatement = connection.prepareStatement(
                    "select id, name, sex, age, course, school, user_id from teacher where 1=1" +
                            (null != user_id ? " and user_id = ?": "")
            );
            if (user_id != null) {
                preparedStatement.setInt(1, user_id);
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            List<Teacher> teacherList = new ArrayList<>();
            while (resultSet.next()) {
                teacherList.add(Teacher.mapResult(resultSet));
            }
            resultSet.close();
            return teacherList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean updateById(Teacher teacher) {
        try (Connection connection = JdbcUtil.getConnection()) {
            PreparedStatement preparedStatement = connection.prepareStatement(
                    "update teacher set name = ?, age = ?, sex =? , school = ?, course = ? where id = ?");
            preparedStatement.setString(1, teacher.getName());
            preparedStatement.setInt(2, teacher.getAge());
            preparedStatement.setString(3, teacher.getSex());
            preparedStatement.setString(4, teacher.getSchool());
            preparedStatement.setString(5, teacher.getCourse());
            preparedStatement.setInt(6, teacher.getId());
            return preparedStatement.execute();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static Teacher getByUserId(Integer userId) {
        try (Connection connection = JdbcUtil.getConnection()) {
            PreparedStatement preparedStatement = connection.prepareStatement("select id, name, sex, age, course, school, user_id from teacher where user_id = ?");
            preparedStatement.setInt(1, userId);
            ResultSet resultSet = preparedStatement.executeQuery();
            Teacher teacher = null;
            while (resultSet.next()) {
                teacher = Teacher.mapResult(resultSet);
                break;
            }
            resultSet.close();
            return teacher;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean deleteById(Integer id) {
        try (Connection connection = JdbcUtil.getConnection()) {
            PreparedStatement preparedStatement = connection.prepareStatement("delete from teacher where id = ?");
            preparedStatement.setInt(1, id);
            boolean flag = preparedStatement.execute();
            preparedStatement.close();
            return flag;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean saveTeacher(Teacher teacher) {
        try (Connection connection = JdbcUtil.getConnection()) {
            PreparedStatement preparedStatement = connection.prepareStatement("insert into teacher(name, sex, age, course, school, user_id) values (?, ?, ?, ?, ?, ?)");
            preparedStatement.setString(1, teacher.getName());
            preparedStatement.setString(2, teacher.getSex());
            preparedStatement.setInt(3, teacher.getAge());
            preparedStatement.setString(4, teacher.getCourse());
            preparedStatement.setString(5, teacher.getSchool());
            preparedStatement.setInt(6, teacher.getUserId());
            boolean flag = preparedStatement.execute();
            preparedStatement.close();
            return flag;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
